/***************************************************************************
 *   Copyright (C) 2006 by Sergey Melderis   *
 *   sergey.melderis@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "browserpopupmenu.h"

#include <qmap.h>
#include <kconfig.h>
#include <qfile.h>
#include <qpopupmenu.h>
#include <kiconloader.h>
#include <qiconset.h>        
#include <kicontheme.h>        
#include "filesprefs.h"
BrowserPopupMenu::BrowserPopupMenu(QObject *parent, const char *name)
 : QObject(parent, name)
{
    mMenu = new QPopupMenu(0);
    mMenu->insertItem("Translate", TRANSLATE_ID);
    mMenu->insertSeparator();
    mFileMenu = new QPopupMenu(mMenu);
    mMenu->insertItem("Add to", mFileMenu, ADD_TO_ID);

    connect (mMenu, SIGNAL (activated(int)), this, SLOT(emitTranslate(int)));

    connect(mMenu, SIGNAL(aboutToShow()), this, SLOT(setUpContent()));
}


BrowserPopupMenu::~BrowserPopupMenu()
{
    delete mMenu;
}

void BrowserPopupMenu::reconfigure()
{
    mFileMenu->clear();
    QMap<QString, QString> map = FilesPrefs::self().keyUrlItems();
    KIconLoader iconLoader;
    QIconSet iconSet = iconLoader.loadIconSet("kwordquiz_doc", KIcon::Small, 0);        

    if (map.size() != 0) {
        QMap<QString, QString>::ConstIterator iter;
        for (iter = map.constBegin(); iter != map.constEnd(); ++iter) {
            mFileMenu->insertItem(iter.key(), this, SLOT(emitAddToFile(int)));;
        }
        mFileMenu->insertSeparator();
    }
    
    mFileMenu->insertItem(iconSet, "Add KWordQuiz files", this, SIGNAL(requestAddWordQuizFiles()));
}

void BrowserPopupMenu::setCurrentWord(const QString &word)
{
    mCurrentWord = word;
}

void BrowserPopupMenu::setCurrentTranslation(const QString &transl)
{
    mCurrentTranslation = transl;
}

void BrowserPopupMenu::emitTranslate(int id)
{
    if (id == TRANSLATE_ID)
        emit translateActivated(mCurrentTranslation);
}


void BrowserPopupMenu::emitAddToFile(int id)
{
    QString name = mFileMenu->text(id);
    emit addToFileActivated(name, mCurrentWord, mCurrentTranslation);
}

void BrowserPopupMenu::setUpContent()
{
    if (mCurrentWord != QString::null) {
        mMenu->setItemEnabled(ADD_TO_ID, true);
        mMenu->changeItem(ADD_TO_ID, "Add  \"" + mCurrentWord + " - " + mCurrentTranslation + "\"  to");
    } else {
        mMenu->setItemEnabled(ADD_TO_ID, false);
    }
}

QPopupMenu * BrowserPopupMenu::popupMenu()
{
    return mMenu;
}

#include "browserpopupmenu.moc"
